﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateMaterialTypeHandler : ICommandHandler<CreateOrUpdateMaterialTypeCommand>
    {
        private readonly IMaterialTypeRepository MaterialTypeRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateMaterialTypeHandler(IMaterialTypeRepository MaterialTypeRepository, IUnitOfWork unitOfWork)
        {
            this.MaterialTypeRepository = MaterialTypeRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateMaterialTypeCommand command)
        {
            var MaterialType = new MaterialType
            {
                ID = command.ID,
                Description = command.Description,
                Name = command.Name,
                Code = command.Code,
                UOMID=command.UOMID,
                
            };
            if (MaterialType.ID == 0)
                MaterialTypeRepository.Add(MaterialType);
            else
                MaterialTypeRepository.Update(MaterialType);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateMaterialTypeCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateMaterialTypeCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
